package cn.sumpu.app.coolsk.web.mvc.misc;

import java.io.IOException;
import java.io.PrintWriter;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import net.sf.json.JSONObject;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;

import cn.sumpu.app.coolsk.biz.ConfigBiz;
import cn.sumpu.app.coolsk.biz.DiscussBiz;
import cn.sumpu.app.coolsk.biz.MemberBiz;
import cn.sumpu.app.coolsk.biz.SyslogBiz;
import cn.sumpu.app.coolsk.constant.Table;
import cn.sumpu.app.coolsk.domain.Config;
import cn.sumpu.app.coolsk.domain.Discuss;
import cn.sumpu.app.coolsk.domain.Member;
import cn.sumpu.app.coolsk.domain.Syslog;
import cn.sumpu.app.coolsk.exception.AppBusinessException;
import cn.sumpu.app.coolsk.web.util.AdminUtil;
import cn.sumpu.toolkit.common.page.PageModel;
import cn.sumpu.toolkit.common.util.Common;

@Controller
public class DisCommentController {
	
	private final static Logger logger = LoggerFactory.getLogger(HotelController.class);
	
	@Autowired
	private DiscussBiz discussBiz;
	@Autowired
	private ConfigBiz configBiz;
	@Autowired
	private MemberBiz memberBiz;
	@Autowired
	private SyslogBiz syslogBiz;
	
	@RequestMapping(value ="/misc/discomment/discomment.htm")
	public String discomment(HttpServletRequest request, HttpServletResponse response) throws IOException {
		return "misc/discomment/discuss";
	}
	
	@RequestMapping(value ="/misc/discomment/discuss.htm")
	public String discuss(String memberName,Integer page,Integer pageSize, HttpServletRequest request,String hotelId,
			HttpServletResponse response) throws IOException {
		response.setContentType("text/html; charset=UTF-8");
		PrintWriter pw = null; 
		try {
			if (page == null) {
				page = 1;
			}    
			PageModel pageModelDiscuss =new PageModel();
			if(Common.emptyStr(memberName)){  
				 pageModelDiscuss = discussBiz.getAllDiscuss(pageSize, page,null);
			}else{
				Member member =memberBiz.getMemberNickName(memberName);
				if(member!=null){
					pageModelDiscuss = discussBiz.getAllDiscuss(pageSize, page,member.getId());
				}
			}
			pw = response.getWriter();
			pw.print(JSONObject.fromObject(pageModelDiscuss));
		} catch (Exception e) {
			pw = response.getWriter();
			pw.print("error");
			if (logger.isErrorEnabled()) {
				logger.error("Add user failure.", e);
			}
		}
		return null;
	}
	@RequestMapping(value ="/misc/discomment/aduitDis.htm")
	public String aduitDis(String id,HttpServletRequest request, HttpServletResponse response) throws IOException {
		try {
			Discuss discuss =discussBiz.getDiscussById(id);
			request.setAttribute("discuss", discuss);
		} catch (AppBusinessException e) {
			e.printStackTrace();
		}
		return "misc/discomment/aduitDis";
	}
	@RequestMapping(value ="/misc/discomment/aduitFlase.htm")
	public String aduitFlase(String id,HttpServletRequest request, HttpServletResponse response) throws IOException {
		response.setContentType("text/html; charset=UTF-8"); 
		PrintWriter pw = null; 
		try {
			//02.审核不通过
			String isAudit="02";
			discussBiz.updateDisById(id,isAudit);
			//记录操作日志
			syslogBiz.addSyslog(new Syslog(AdminUtil.getUsername(), AdminUtil.getRealname(), Syslog.update, Table.DISCUSS, id));
			pw = response.getWriter();
			pw.print("success");
		} catch (Exception e) {
			pw = response.getWriter();
			pw.print("error");
			if (logger.isErrorEnabled()) {
				logger.error(" aduitFlase failure.", e);
			}
		}
		return null;
	}
	@RequestMapping(value ="/misc/discomment/aduitTrue.htm")
	public String aduitTrue(String id,String member,HttpServletRequest request, HttpServletResponse response) throws IOException {
		response.setContentType("text/html; charset=UTF-8"); 
		PrintWriter pw = null; 
		try {
			//02.审核不通过  
			String isAudit="01";
			discussBiz.updateDisById(id,isAudit);
			//记录操作日志
			syslogBiz.addSyslog(new Syslog(AdminUtil.getUsername(), AdminUtil.getRealname(), Syslog.update, Table.DISCUSS, id));
			//config key标识disPoint取对应值
			Config config=configBiz.getConfigById("disPoint");
			String point=config.getValue();  
			Integer pointInt=Integer.parseInt(point);
			discussBiz.pointByAduitDis(pointInt,member);
			pw = response.getWriter();
			pw.print("success");
		} catch (Exception e) {
			pw = response.getWriter();
			pw.print("error");
			if (logger.isErrorEnabled()) {
				logger.error(" aduitFlase failure.", e);
			}
		}
		return null;
	}
}
